using System;
using System.Collections.Generic;
using Clandestine.Engines.Field;

namespace Shlick.LayerModel
{
	internal class ObjectItem : Item
	{
		public Clandestine.Engines.Field.Object Object { get; private set; }
		
		private ObjectItem(Clandestine.Engines.Field.Object item, MapDocument mapDocument, LayerTreeMode mode) : base(item, mapDocument, mode)
		{
			this.Object = item;
		}
		
		public static ObjectItem Get(Clandestine.Engines.Field.Object item, MapDocument mapDocument, LayerTreeMode mode)
		{
			if (cachedItems.ContainsKey(mode) && cachedItems[mode].ContainsKey(item))
				return (ObjectItem)cachedItems[mode][item];
			else
				return new ObjectItem(item, mapDocument, mode);
		} 
		
		public override int Row()
		{
			ObjectList list = this.Object.Level.Objects;
			for (int i=0; i < list.Count; i++)
				if (list[i] == this.Object)
					return i;
			throw new Exception();
		}
		
		public override Item Child(int row)
		{
			// Rows: 0 - Sky, 1 - Perp, 2 - Floor
			switch (row)
			{
				case 0:
					return LayerGroupItem.Get(this.Object.SkyLayers, this.MapDocument, this.Mode);
				case 1: 
					return LayerGroupItem.Get(this.Object.PerpendicularLayers, this.MapDocument, this.Mode);
				case 2:
					return LayerGroupItem.Get(this.Object.FloorLayers, this.MapDocument, this.Mode);
			}
			throw new Exception();
		}
		
		public override Item Parent()
		{
			return null;
		}
		
		public override int ChildrenCount()
		{
			return 3;
		}
		
		public override string GetData()
		{
			 return String.Format("Object, Name: {0}", this.Object.Name);
		}
		
		public override string GetName()
		{
			return this.Object.Name;
		}
		
		public override bool? IsVisible()
		{
			 return this.Object.Visible;
		}
		
		public override bool SetVisible(bool visible)
		{
			this.Object.Visible = visible;
			return true;
		}
	}
}